package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Menu;
import com.itheima.pojo.Permission;
import com.itheima.pojo.Role;
import com.itheima.service.PermissionService;
import com.itheima.service.RoleService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 角色维护
 */
@RestController
@RequestMapping("/role")
public class RoleController {
    @Reference
    private RoleService roleService;

    //添加前，查询权限
    @RequestMapping("/findAllPermission")
    public Result findAllPermission(){
            List<Permission> permissionList = roleService.findAllPermission();
            if(permissionList != null && permissionList.size() >0){
                return new Result(true, "查询权限信息成功",permissionList);
            }
            return new Result(false, "查询权限信息失败");
    }
    //添加前，查询菜单
    @RequestMapping("/findAllMenu")
    public Result findAllMenu(){
        List<Menu> menuList = roleService.findAllMenu();
        if(menuList != null && menuList.size() >0){
            return new Result(true, "查询菜单信息成功",menuList);
        }
        return new Result(false, "查询菜单信息失败");
    }
    //新增角色
    @RequestMapping("/add")
    public Result add(@RequestBody Role role, Integer[] permissionIds,Integer[] menuIds){
        try{
            roleService.add(role,permissionIds,menuIds);
            return new Result(true, "新增角色成功");
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false, "新增角色失败");
        }
    }
    //分页查询
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean pageBean){
        return roleService.findPage(pageBean);
    }
    //编辑前，根据id查角色信息
    @RequestMapping("/findById")
    public Result findById(Integer id){
        try{
            Role role = roleService.findById(id);
            return new Result(true, "查询角色成功",role);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false, "查询角色失败");
        }
    }
    //编辑前，根据id查询权限信息
    @RequestMapping("/findPermissionIdsByRoleId")
    public Result findPermissionIdsByRoleId(Integer roleId){
        try{
            List<Integer> permissionIds = roleService.findPermissionIdsByRoleId(roleId);
            return new Result(true, "查询权限成功",permissionIds);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false, "查询权限失败");
        }
    }
    //编辑前，根据id查询菜单信息
    @RequestMapping("/findMenuIdsByRoleId")
    public Result findMenuIdsByRoleId(Integer roleId){
        try{
            List<Integer> menuIds = roleService.findMenuIdsByRoleId(roleId);
            return new Result(true, "查询菜单成功",menuIds);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false, "查询菜单失败");
        }
    }
    //编辑角色
    @RequestMapping("/edit")
    public Result edit(@RequestBody Role role, Integer[] permissionIds,Integer[] menuIds){
        try{
            roleService.edit(role,permissionIds,menuIds);
            return new Result(true, "编辑角色信息成功");
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false, "编辑角色信息失败");
        }
    }
    //根据id删除j角色
    @RequestMapping("/delete")
    public Result delete(Integer id){
        try{
            roleService.delete(id);//发送请求
            return new Result(true, "删除角色成功");
        }catch (Exception e){
            String message = e.getMessage();
            e.printStackTrace();
            return new Result(false, message);
        }
    }

}
